package com.yuexunit.operationman.nbsy.module.InterventionalOperation.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.FlaxmeterBean;
import com.yuexunit.operationman.nbsy.module.InterventionalOperation.Bean.InterNursingBean;
import com.yuexunit.operationman.nbsy.widget.FlaxMeterDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/28 0028.
 */

public class Act_InterventionalOperation_FlaxMeter extends BaseKJActivity {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    @Bind(R.id.preoperative_rate)
    TextView preoperativeRate;
    @Bind(R.id.preoperative_bp)
    TextView preoperativeBp;
    @Bind(R.id.preoperative_saturation)
    TextView preoperativeSaturation;
    @Bind(R.id.item_view)
    LinearLayout itemView;
    @Bind(R.id.postoperation_rate)
    TextView postoperationRate;
    @Bind(R.id.postoperation_bp)
    TextView postoperationBp;
    @Bind(R.id.postoperation_saturation)
    TextView postoperationSaturation;
    @Bind(R.id.preoperative_time)
    TextView preoperativeTime;
    @Bind(R.id.postoperation_time)
    TextView postoperationTime;

    List<FlaxmeterBean> beanList = new ArrayList<>();
    FlaxmeterBean bean;
    FlaxMeterDialog dialog;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_interventionaloperation_flaxmeter);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        tvSubmit.setText("确认");
        setContent();

    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getPatientName());
        if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.INTERNURSEBEAN.getObject().getInterNursing().getOperationName());
    }

    private void setContent() {
        if(AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList() != null)
        {
            String[] P = new String[0],BP = new String[0],OS = new String[0],Time = new String[0];
            for (InterNursingBean.ObjectEntity.InterNursingItemListEntity p : AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList()) {
                switch (p.getItemCode()) {
                    case "P":
                        if (p.getItemValue().length() > 0) {
                            if(p.getItemValue().substring(p.getItemValue().length()-1,p.getItemValue().length()).equals(";"))
                            {
                                P = p.getItemValue().substring(0,p.getItemValue().length()-1).split(";", -1);
                            }
                            else {
                                P =  p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                    case "BP":
                        if (p.getItemValue().length() > 0) {
                            if (p.getItemValue().substring(p.getItemValue().length() - 1, p.getItemValue().length()).equals(";")) {
                                BP = p.getItemValue().substring(0, p.getItemValue().length() - 1).split(";", -1);
                            } else {
                                BP = p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                    case "OS":
                        if (p.getItemValue().length() > 0) {
                            if (p.getItemValue().substring(p.getItemValue().length() - 1, p.getItemValue().length()).equals(";")) {
                                OS = p.getItemValue().substring(0, p.getItemValue().length() - 1).split(";", -1);
                            } else {
                                OS = p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                    case "Time":
                        if (p.getItemValue().length() > 0) {
                            if (p.getItemValue().substring(p.getItemValue().length() - 1, p.getItemValue().length()).equals(";")) {
                                Time = p.getItemValue().substring(0, p.getItemValue().length() - 1).split(";", -1);
                            } else {
                                Time = p.getItemValue().split(";", -1);
                            }
                        }
                        break;
                }
            }

            for (int i = 0;i<Time.length;i++)
            {
                bean = new FlaxmeterBean();
                bean.setTime(Time[i]);
                if( BP.length>i&&BP[i].length() > 0) {
                    String[] skin = BP[i].split("/", -1);
                    bean.setDbp(skin[0]);
                    bean.setSbp(skin[1]);
                }
                else {
                    bean.setDbp("");
                    bean.setSbp("");
                }
                if(P.length>i) {
                    bean.setRate(P[i]);
                }
                else {
                    bean.setRate("");
                }
                if(OS.length>i) {
                    bean.setSaturation(OS[i]);
                }
                else {
                    bean.setSaturation("");
                }
                beanList.add(bean);
            }

            if(beanList.size()>0) {
                for (int i = 1; i < beanList.size() - 1; i++) {
                    itemView.addView(new AddFlaxMeterItem(aty).itemView(beanList.get(i)));
                }
                preoperativeRate.setText(beanList.get(0).getRate());
                preoperativeBp.setText(beanList.get(0).getDbp() + "/" + beanList.get(0).getSbp());
                preoperativeSaturation.setText(beanList.get(0).getSaturation());

                postoperationRate.setText(beanList.get(beanList.size() - 1).getRate());
                postoperationBp.setText(beanList.get(beanList.size() - 1).getDbp() + "/" + beanList.get(beanList.size() - 1).getSbp());
                postoperationSaturation.setText(beanList.get(beanList.size() - 1).getSaturation());
            }
            else {
                bean = new FlaxmeterBean();
                bean.setTime("术前");
                bean.setDbp("");
                bean.setSbp("");
                bean.setRate("");
                bean.setSaturation("");
                beanList.add(bean);
                bean = new FlaxmeterBean();
                bean.setTime("术后");
                bean.setDbp("");
                bean.setSbp("");
                bean.setRate("");
                bean.setSaturation("");
                beanList.add(bean);
            }
        }
        else {
            bean = new FlaxmeterBean();
            bean.setTime("术前");
            bean.setDbp("");
            bean.setSbp("");
            bean.setRate("");
            bean.setSaturation("");
            beanList.add(bean);
            bean = new FlaxmeterBean();
            bean.setTime("术后");
            bean.setDbp("");
            bean.setSbp("");
            bean.setRate("");
            bean.setSaturation("");
            beanList.add(bean);
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.tv_submit,
            R.id.preoperative_rate, R.id.preoperative_bp,
            R.id.preoperative_saturation, R.id.add_item, R.id.postoperation_rate,
            R.id.postoperation_bp, R.id.postoperation_saturation})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.tv_submit:
                insertContent();
                finish();
                break;
            case R.id.preoperative_rate:
                dialog = new FlaxMeterDialog(aty, false).builder();
                dialog.setTitle("请输入心率值", "原时间" + preoperativeTime.getText().toString());
                dialog.setDtata(preoperativeRate.getText().toString(), 220);
                dialog.setCallBack(new FlaxMeterDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        beanList.get(0).setRate(bundle.getString("content"));
                        preoperativeRate.setText(bundle.getString("content"));
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.preoperative_bp:
                dialog = new FlaxMeterDialog(aty, true).builder();
                dialog.setTitle("请输入血压值", "原时间" + preoperativeTime.getText().toString());
                dialog.setBp(beanList.get(0).getSbp(), beanList.get(0).getDbp());
                dialog.setCallBack(new FlaxMeterDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        beanList.get(0).setDbp(bundle.getString("dbp"));
                        beanList.get(0).setSbp(bundle.getString("sbp"));
                        preoperativeBp.setText(bundle.getString("dbp") + "/" + bundle.getString("sbp"));
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.preoperative_saturation:
                dialog = new FlaxMeterDialog(aty, false).builder();
                dialog.setTitle("请输入氧饱和度", "原时间" + preoperativeTime.getText().toString());
                dialog.setDtata(preoperativeSaturation.getText().toString(), 100);
                dialog.setCallBack(new FlaxMeterDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        beanList.get(0).setSaturation(bundle.getString("content"));
                        preoperativeSaturation.setText(bundle.getString("content"));
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.add_item:
                bean = new FlaxmeterBean();
                if (beanList.size() == 2) {
                    bean.setTime("10");
                } else if (beanList.size() > 2) {
                    int lastTime = Integer.valueOf(beanList.get(beanList.size() - 2).getTime());
                    if ((lastTime + 10) > 60) {
                        bean.setTime((lastTime + 10 - 60) + "");
                    } else {
                        bean.setTime((lastTime + 10) + "");
                    }
                }
                bean.setDbp("");
                bean.setSbp("");
                bean.setRate("");
                bean.setSaturation("");
                itemView.addView(new AddFlaxMeterItem(aty).itemView(bean));
                beanList.add(beanList.size() - 1, bean);
                break;
            case R.id.postoperation_rate:
                dialog = new FlaxMeterDialog(aty, false).builder();
                dialog.setTitle("请输入心率值", "原时间" + postoperationTime.getText().toString());
                dialog.setDtata(postoperationRate.getText().toString(), 220);
                dialog.setCallBack(new FlaxMeterDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        beanList.get(beanList.size()-1).setRate(bundle.getString("content"));
                        postoperationRate.setText(bundle.getString("content"));
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.postoperation_bp:
                dialog = new FlaxMeterDialog(aty, true).builder();
                dialog.setTitle("请输入血压值", "原时间" + postoperationTime.getText().toString());
                dialog.setBp(beanList.get(beanList.size()-1).getSbp(), beanList.get(beanList.size()-1).getDbp());
                dialog.setCallBack(new FlaxMeterDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        beanList.get(beanList.size()-1).setDbp(bundle.getString("dbp"));
                        beanList.get(beanList.size()-1).setSbp(bundle.getString("sbp"));
                        postoperationBp.setText(bundle.getString("dbp") + "/" + bundle.getString("sbp"));
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.postoperation_saturation:
                dialog = new FlaxMeterDialog(aty, false).builder();
                dialog.setTitle("请输入氧饱和度", "原时间" + postoperationTime.getText().toString());
                dialog.setDtata(postoperationSaturation.getText().toString(), 100);
                dialog.setCallBack(new FlaxMeterDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        beanList.get(beanList.size()-1).setSaturation(bundle.getString("content"));
                        postoperationSaturation.setText(bundle.getString("content"));
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
        }
    }

    private void insertContent() {
        for (int i = 0; i < AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList().size(); i++) {
            switch (AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList().get(i).getItemCode()) {
                case "P":
                    String p="";
                    for (int j =0 ;j<beanList.size();j++)
                    {
                        p+=beanList.get(j).getRate()+";";
                    }
                    AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList().get(i).setItemValue(p);
                    break;
                case "BP":
                    String bp="";
                    for (int j =0 ;j<beanList.size();j++)
                    {
                        bp+=beanList.get(j).getDbp()+"/"+beanList.get(j).getSbp()+";";
                    }
                    AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList().get(i).setItemValue(bp);
                    break;
                case "OS":
                    String os="";
                    for (int j =0 ;j<beanList.size();j++)
                    {
                        os+=beanList.get(j).getSaturation()+";";
                    }
                    AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList().get(i).setItemValue(os);
                    break;
                case "Time":
                    String time="";
                    for (int j =0 ;j<beanList.size();j++)
                    {
                        time+= beanList.get(j).getTime()+";";
                    }
                    AppConfig.INTERNURSEBEAN.getObject().getInterNursingItemList().get(i).setItemValue(time);
                    break;
            }
        }
    }
}
